package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Map; 
import org.springframework.test.context.ContextConfiguration; 
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.opensymphony.xwork2.ActionContext; 
import org.springframework.context.ApplicationContext;
import com.lb.jwitter.util.ApplicationContextProvider; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;
import com.lb.jwitter.service.TblQLDTTXANamHoc;
import com.lb.jwitter.service.TblQLDTTXANamHocService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXANamHoctblQLDTTXAHocKyTrongNamInsertAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public void setId(Long id) {
       this.id = id;
 }

    private String ten; 
    public void setTen(String ten) {
       this.ten = ten;
 }

    public String getTen() {
       return ten;
 }

    private int namHocId; 
    public void setNamHocId(int namHocId) {
       this.namHocId = namHocId;
 }

    public int getNamHocId() {
       return namHocId;
 }

    private TblQLDTTXANamHocService tblQLDTTXANamHocService; 

    private String thoiGianBatDau; 
    public void setThoiGianBatDau(String thoiGianBatDau) {
       this.thoiGianBatDau = thoiGianBatDau;
 }

    public String getThoiGianBatDau() {
       return thoiGianBatDau;
 }

    private String thoiGianKetThuc; 
    public void setThoiGianKetThuc(String thoiGianKetThuc) {
       this.thoiGianKetThuc = thoiGianKetThuc;
 }

    public String getThoiGianKetThuc() {
       return thoiGianKetThuc;
 }

    private int tongTuanHoc; 
    public void setTongTuanHoc(int tongTuanHoc) {
       this.tongTuanHoc = tongTuanHoc;
 }

    public int getTongTuanHoc() {
       return tongTuanHoc;
 }

    private int tuanThiGiuaKy; 
    public void setTuanThiGiuaKy(int tuanThiGiuaKy) {
       this.tuanThiGiuaKy = tuanThiGiuaKy;
 }

    public int getTuanThiGiuaKy() {
       return tuanThiGiuaKy;
 }

    private int tuanKTThiGiuaKy; 
    public void setTuanKTThiGiuaKy(int tuanKTThiGiuaKy) {
       this.tuanKTThiGiuaKy = tuanKTThiGiuaKy;
 }

    public int getTuanKTThiGiuaKy() {
       return tuanKTThiGiuaKy;
 }

    private int tuanThiCuoiKy; 
    public void setTuanThiCuoiKy(int tuanThiCuoiKy) {
       this.tuanThiCuoiKy = tuanThiCuoiKy;
 }

    public int getTuanThiCuoiKy() {
       return tuanThiCuoiKy;
 }

    private TblQLDTTXAHocKyTrongNam tblQLDTTXAHocKyTrongNam; 
    public TblQLDTTXAHocKyTrongNam  getTblQLDTTXAHocKyTrongNam() {
        return tblQLDTTXAHocKyTrongNam;
 }

    private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams; 
    public List<TblQLDTTXAHocKyTrongNam>  getTblQLDTTXAHocKyTrongNams() {
        return tblQLDTTXAHocKyTrongNams; 
      } 

    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 

	private int preInsert(Message msg, TblQLDTTXAHocKyTrongNam obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblQLDTTXAHocKyTrongNam obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
    public String execute() throws Exception 
   { 
    try{ 
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocKyTrongNam", "them")){ 
        return "noprivilege"; 
    }
        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 
        tblQLDTTXANamHocService = (TblQLDTTXANamHocService) context.getBean("tbl_QLDTTXA_NamHocClient"); 

        msg = new Message(); 
        tblQLDTTXAHocKyTrongNam = new TblQLDTTXAHocKyTrongNam(); 
        tblQLDTTXAHocKyTrongNam.setTen(ten); 
        tblQLDTTXAHocKyTrongNam.setNamHocId(tblQLDTTXANamHocService.gettblQLDTTXANamHoc(new Long(namHocId))); 
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
       Date dd = df.parse(thoiGianBatDau); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblQLDTTXAHocKyTrongNam.setThoiGianBatDau(d); 

        dd = df.parse(thoiGianKetThuc); 
        c = new GregorianCalendar(); 
       c.setTime(dd);
        d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblQLDTTXAHocKyTrongNam.setThoiGianKetThuc(d); 
        tblQLDTTXAHocKyTrongNam.setTongTuanHoc(tongTuanHoc); 
        tblQLDTTXAHocKyTrongNam.setTuanThiGiuaKy(tuanThiGiuaKy); 
        tblQLDTTXAHocKyTrongNam.setTuanKTThiGiuaKy(tuanKTThiGiuaKy); 
        tblQLDTTXAHocKyTrongNam.setTuanThiCuoiKy(tuanThiCuoiKy); 
        preInsert(msg,tblQLDTTXAHocKyTrongNam); 
       if (msg.isPreMessage_b()) 
       { 
           tblQLDTTXAHocKyTrongNamService.createtblQLDTTXAHocKyTrongNam(tblQLDTTXAHocKyTrongNam); 
           postInsert(msg, tblQLDTTXAHocKyTrongNam); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
        List < ConditionReport > cons = new ArrayList<ConditionReport>(); 
        ConditionReport con = new ConditionReport(); 
        con.setCol("namHocId.id"); 
        con.setValue1(new Long(namHocId)); 
        con.setOperator("eq"); 
        cons.add(con); 
      tblQLDTTXAHocKyTrongNams =       tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons,pageSize, page); 
      pages = tblQLDTTXAHocKyTrongNamService.counttblQLDTTXAHocKyTrongNamByCondition(cons)/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&ten="; 
      dataString += "&namHocId="; 
      dataString += "&thoiGianBatDau="; 
      dataString += "&thoiGianKetThuc="; 
      dataString += "&tongTuanHoc="; 
      dataString += "&tuanThiGiuaKy="; 
      dataString += "&tuanKTThiGiuaKy="; 
      dataString += "&tuanThiCuoiKy="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_QLDTTXA_HocKyTrongNam Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
